package com.gzhu.knowledgeAdmin.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzhu.knowledgeAdmin.common.constant.Constants;
import com.gzhu.knowledgeAdmin.common.result.Result;
import com.gzhu.knowledgeAdmin.common.utils.PageableUtil;
import com.gzhu.knowledgeAdmin.common.utils.UserInfoUtils;
import com.gzhu.knowledgeAdmin.dao.TBqTMapper;
import com.gzhu.knowledgeAdmin.entity.TBqT;
import com.gzhu.knowledgeAdmin.entity.User;
import com.gzhu.knowledgeAdmin.service.TBqTService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class TBqTServiceImpl extends ServiceImpl<TBqTMapper, TBqT> implements TBqTService {
    @Resource
    private TBqTMapper tBqTMapper;

    @Override
    public Result selectAllBySearch(TBqT tBqT) {
        try {
            PageableUtil.setupPageable(tBqT, Constants.PAGE_SIZE);
            List<TBqT> tBqTs = tBqTMapper.selectAllBySearch(tBqT);

            Integer total;
            if ((tBqT.getFBqmc() == null || tBqT.getFBqmc().isEmpty())) {
                total = tBqTMapper.selectCount();
            } else {
                total = tBqTMapper.selectCountBySearch(tBqT);
            }

            HashMap<String, Object> res = new HashMap<>();
            res.put("data", tBqTs);
            res.put("total", total);

            return Result.success(res);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constants.CODE_500, e.getMessage());
        }
    }

    @Override
    public Integer selectCount() {
        return tBqTMapper.selectCount();
    }

    @Override
    public Integer selectCountBySearch(TBqT tBqT) {
        return tBqTMapper.selectCountBySearch(tBqT);
    }

    @Override
    public Integer addLabel(TBqT tBqT) {
        Date now = new Date();
        tBqT.setFCjsj(now);
        tBqT.setFGxsj(now);
        tBqT.setFBqlb(2);
        User currentUser = UserInfoUtils.getCurrentUser();
        tBqT.setFGsid(currentUser.getGsid());
        return tBqTMapper.insertLabel(tBqT);
    }

    @Override
    public Integer updateLabel(TBqT tBqT) {
        Date now = new Date();
        tBqT.setFGxsj(now);
        return tBqTMapper.updateLabel(tBqT);
    }
}
